package com.wj.infrastructure.mapper;

import com.wj.tool.config.wx.UserResponseDesVO;
import org.apache.ibatis.annotations.*;


/**
 * 微信授权信息数据接口
 *
 * @description:
 * @author: zlf
 * @Date: 2020-09
 */
@Mapper
public interface WeChatUserMapper{
    /**
     * 根据ID获取微信用户信息
     *
     * @param openId 微信openId
     * @return
     */
    @Results(
            id = "userInfo-map",
            value = {
                    @Result(property = "id", column = "id"),
                    @Result(property = "identityCard", column = "identity_card"),
                    @Result(property = "openId", column = "open_id"),
                    @Result(property = "nickName", column = "nick_name"),
                    @Result(property = "mobile", column = "mobile"),
                    @Result(property = "avatarUrl", column = "avatar_url"),
                    @Result(property = "country", column = "country"),
                    @Result(property = "province", column = "province"),
                    @Result(property = "city", column = "city"),
                    @Result(property = "gender", column = "gender"),
                    @Result(property = "language", column = "language"),
                    @Result(property = "name", column = "name"),
            }
    )
    @Select({" select * ",
            " from prop_wechat_user ",
            " where open_id = #{openId} "})
    WeChatUserDO getWeChatUserByOpenId(@Param("openId") String openId);


    @Select({" SELECT wechat.identity_card AS identityCard,wechat.mobile AS phone,wechat.name AS name, ",
            "wechat.open_id AS openid,wechat.nick_name AS nickName,wechat.avatar_url AS avatarUrl,wechat.country,wechat.province,wechat.city,wechat.gender,wechat.language",
            " FROM prop_wechat_user wechat ",
            " WHERE wechat.open_id = #{openId} "})
    UserResponseDesVO getWeChatUserByCommunityId(@Param("openId") String openId);


    @Select({" SELECT estate.name ",
            " FROM  prop_wechat_user estate",
            " WHERE estate.open_id = #{openId}  "})
    String getWeChatByUserName(@Param("openId") String openId);


    /**
     * 根据identityCard获取微信用户信息
     *
     * @param identityCard identityCard
     * @return
     */
    @ResultMap(value = "userInfo-map")
    @Select({" select * ",
            " from prop_wechat_user ",
            " where identity_card = #{identityCard} "})
    WeChatUserDO getWeChatUserByIdentityCard(@Param("identityCard") String identityCard);


    /**
     * 保存微信授权关联信息
     *
     * @param weChatUserDO
     */
    @Insert({" insert into prop_wechat_user ",
            " (id,open_id,nick_name,mobile,avatar_url,country,province,city,gender,language,create_time,update_time) ",
            " values(#{id},#{openId},#{nickName},#{mobile}",
            ",#{avatarUrl},#{country},#{province},#{city},#{gender},#{language},#{createTime},#{updateTime})"})
    void save(WeChatUserDO weChatUserDO);


    /**
     * 修改微信用户信息
     *
     * @param openId       微信openId
     * @param identityCard 身份证号
     */
    @Update({"update prop_wechat_user ",
            " set identity_card = #{identityCard}",
            " ,name = #{name},mobile=#{mobile} ",
            " where open_id = #{openId}"})
    void updateUserIdentityCard(@Param("openId") String openId
            , @Param("identityCard") String identityCard, @Param("name") String name, @Param("mobile") String mobile);


}
